<?php

require_once('Component.php');
class Antz_Form_Fieldset extends Antz_Form_Component
{
    protected $components = array();
    protected $classnames = array();
    protected $showBorder;
    protected $altTag;
    protected $id;

    public function __construct($legend){
        $this->legend = $legend;
        $this->showBorder(true);
    }

    public function addLongTag($tagname, $text=''){
        $this->components[] = Antz::newObject('Antz_Form_Components_LongTag', array($tagname, $text));
        $obj = $this->getLastComponent();
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    public function addShortTag($tagname){
        $this->components[] = Antz::newObject('Antz_Form_Components_ShortTag', array($tagname));
        $obj = $this->getLastComponent();
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    public function addInput($type, $name, $label, $value, $afterText=''){
        if($type == 'file') $this->formObj->setEncType('multipart/form-data');
        $this->components[] = Antz::newObject('Antz_Form_Components_Input', array($type, $name, $label, $value, $afterText));
        $obj = $this->getLastComponent();
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    public function addSelect($name, $label, $value='', $aftertext=''){
        $this->components[] = Antz::newObject('Antz_Form_Components_Select', array($name, $label, $value, $aftertext));
        $obj = $this->getLastComponent();
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    public function addDateSelect($name, $label, $value='', $yearStart='', $yearEnd=''){
        $this->components[] = Antz::newObject('Antz_Form_Components_DateSelect', array($name, $label, $value, $yearStart, $yearEnd));
        $obj = $this->getLastComponent();
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }
    public function addBankNumber($name, $label, $value=array()){
        $obj = Antz::newObject('Antz_Form_Components_BankNumber', array($name, $label, $value));
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        $this->components[] = $obj;
        return $obj;
    }

    public function addTimeSelect($name, $label, $value=''){
        $obj = Antz::newObject('Antz_Form_Components_TimeSelect', array($name, $label, $value));
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        $this->components[] = $obj;
        return $obj;
    }
    public function addTimeDateSelect($name, $label, $value=''){
        $obj = Antz::newObject('Antz_Form_Components_TimeDateSelect', array($name, $label, $value));
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        $this->components[] = $obj;
        return $obj;
    }
    public function addCheckboxGroup($name){
        $this->components[] = Antz::newObject('Antz_Form_Components_CheckboxGroup', array($name));
        $obj = $this->getLastComponent();
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    public function addRadioGroup($name, $value=''){
        $this->components[] = Antz::newObject('Antz_Form_Components_RadioGroup', array($name, $value));
        $obj = $this->getLastComponent();
        $obj->setId($this->id.count($this->components));
        $obj->formObj = $this->formObj;
        $obj->blockObj = $this->blockObj;
        $obj->fieldsetObj = $this;
        return $obj;
    }

    public function getLastComponent(){
        return $this->components[(count($this->components)-1)];
    }

    public function countComponents(){
        return count($this->components);
    }

    public function showBorder($bool, $altTag='p'){
        $this->showBorder = (bool) $bool;
        $this->altTag = $altTag;
    }

    public function render(){
        $showBorder = ($this->showBorder) ? '':  ' no-border ' ;
        $legend = '<legend>'.$this->legend.'</legend>';
        $returnString = "\n".'      <fieldset class="'.$showBorder.' '.implode(' ', $this->classnames).'" id="'.$this->getId().'">';
        $returnString .= "\n".'        '.$legend;
        foreach($this->components as $component){
            $returnString .= "\n          ".$component->render().Antz::getFormError($component->name);
        };
        $returnString .= "\n".'      </fieldset>';
        return $returnString;
    }

}
